package com.atzly.community.datasource;

import com.atzly.community.utils.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 支持主从复制的数据库设置配置对象
 */
public class ArrayDataSourceConfig implements Serializable {

    private String dynamicName;

    private static final long serialVersionUID = -3211436249152148184L;
    private String[] nodeIds;

    private List<DataSourceConfig> dataSourceConfigs = new ArrayList<>();

    public ArrayDataSourceConfig(String nodelist) {
        this.nodeIds = StringUtil.split(nodelist, ',');
    }

    public List<DataSourceConfig> getDataSourceConfigs() {
        return dataSourceConfigs;
    }

    public void setDataSourceConfigs(List<DataSourceConfig> dataSourceConfigs) {
        this.dataSourceConfigs = dataSourceConfigs;
    }

    public String[] getNodeIds() {
        return nodeIds;
    }

    public boolean add(DataSourceConfig nodeDataSourceConfig) {
        if (nodeDataSourceConfig != null) {
            return dataSourceConfigs.add(nodeDataSourceConfig);
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder("ArrayDataSourceConfig [url=" + StringUtil.join(nodeIds) + ", dataSourceConfigs [");
        for (DataSourceConfig dataSourceConfig : dataSourceConfigs) {
            str.append(dataSourceConfig.toString()).append("\n");
        }
        str.append("]]");
        return str.toString();
    }

    public String getDynamicName() {
        return dynamicName;
    }

    public void setDynamicName(String dynamicName) {
        this.dynamicName = dynamicName;
    }
}
